/***
 * * PROJECT
 * *    Logical Neighborhood Routing
 * * VERSION
 * *    $Revision: 1.4 $
 * * DATE
 * *    $Date: 2007/01/29 17:48:01 $
 * * LOG
 * *    $Log: IntegerSimplePredicate.java,v $
 * *    Revision 1.4  2007/01/29 17:48:01  lmottola
 * *    Added debug info in toString() method.
 * *
 * *    Revision 1.3  2006/11/03 23:09:35  lmottola
 * *    Changed nodeAttributes package.
 * *
 * *    Revision 1.2  2006/11/03 23:08:18  lmottola
 * *    Traffic application beta running.
 * *
 * *    Revision 1.1  2006/10/24 19:00:59  lmottola
 * *    Added attributes and predicates.
 * *
 * *    Revision 1.1  2006/10/20 23:54:13  lmottola
 * *    Initial LN code in.
 * *
 * *    Revision 1.2  2006/10/19 21:21:52  lmottola
 * *    Added CVS preamble.
 * *
 * *
 * *   Logical Neighborhoods Routing - Java Version
 * *                                
 * *
 * *   This program is free software; you can redistribute it and/or
 * *   modify it under the terms of the GNU General Public License
 * *   as published by the Free Software Foundation; either version 2
 * *   of the License, or (at your option) any later version.
 * *
 * *   This program is distributed in the hope that it will be useful,
 * *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 * *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * *   GNU General Public License for more details.
 * *
 * *   You should have received a copy of the GNU General Public License
 * *   along with this program; if not, you may find a copy at the FSF web
 * *   site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
 * *   Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * *   Boston, MA  02111-1307, USA
 ***/

package atag.runtime.ln.predicates;

import atag.runtime.ln.nodeAttributes.IntegerAttribute;
import atag.runtime.ln.nodeAttributes.NodeAttribute;

public class IntegerSimplePredicate implements LNPredicate {

	// Possible comparators
	public static final int EQUAL = 0;

	public static final int LESS_THAN = 1;

	public static final int GREATER_THAN = 2;

	public static final int LESS_EQUAL_THAN = 3;

	public static final int GREATER_EQUAL_THAN = 4;

	// Predicate data
	private String attrName;

	private int comparator;

	private int value;

	public IntegerSimplePredicate(String attrName, int comparator, int value) {
		this.attrName = attrName;
		this.comparator = comparator;
		this.value = value;
	}

	public IntegerSimplePredicate(String attrName, int comparator, Integer value) {
		this.attrName = attrName;
		this.comparator = comparator;
		this.value = value.intValue();
	}

	public boolean matches(NodeAttribute attr) {

		if (!(attr instanceof IntegerAttribute))
			return false;

		if (!attr.getKey().equals(attrName))
			return false;

		int attrValue = ((Integer) attr.getValue()).intValue();
		switch (comparator) {

		case EQUAL:
			return (attrValue == value);
		case LESS_THAN:
			return (attrValue < value);
		case GREATER_THAN:
			return (attrValue > value);
		case LESS_EQUAL_THAN:
			return (attrValue <= value);
		case GREATER_EQUAL_THAN:
			return (attrValue >= value);
		default:
			return false;
		}
	}

	public String toString() {

		String msg = "IntegerSimple predicate for attribute "+ attrName + " ";

		switch (comparator) {

		case EQUAL:
			msg = msg.concat("=");
			break;
		case LESS_THAN:
			msg = msg.concat("<");
			break;
		case GREATER_THAN:
			msg = msg.concat(">");
			break;
		case LESS_EQUAL_THAN:
			msg = msg.concat("=<");
			break;
		case GREATER_EQUAL_THAN:
			msg = msg.concat("=>");
			break;
		default:
			msg = msg.concat("UNKNOWN");
			break;
		}
		
		return msg.concat(" "+value);
	}
}
